package com.study.it.dubbo;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.study.it.dubbo.face.RecordDubbo;
import com.study.it.entity.Account;
import com.study.it.entity.Record;

//重载HystrixCommand 的getFallback方法实现逻辑
public class RecordAddCommand extends HystrixCommand<Record> {
    private RecordDubbo recordDubboService;
    private Float money;
    private Integer accid;


    public RecordAddCommand(RecordDubbo _service,Float _money,Integer _accid) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("recordGroup"))
                /* 配置依赖超时时间,500毫秒*/
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(500)));
      this.recordDubboService=_service;
      this.money=_money;
      this.accid=_accid;
    }
    @Override
    protected Record getFallback() {//如果发生错误，就返回一个特别的对象
        System.out.println("超时了调用getFallback");
        return null;
    }
    @Override
    protected Record run() throws Exception {
        System.out.println("Hystrix调用getFallback");
        Thread.sleep(1000);
        return  recordDubboService.add(money, accid);
    }

}
